/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.PhieuNoDTO;

/**
 *
 * @author tuan
 */
public class PhieuNoDAO {
    
    private static final String MaPhieuNo = "MaPNo000";
    private static final int lengthMa = 8;
    
    public PhieuNoDAO() {
    }
    
    public String getNewMaPhieuNo() throws SQLException {
        String MaPNo = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllPhieuNoFromDatabase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MAPHIEUNO").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaPNo = MaPNo.concat(MaPhieuNo + maxMa);
        return MaPNo;
    }
        
    public ArrayList<PhieuNoDTO> GetAllPhieuNo() throws SQLException {
        ArrayList<PhieuNoDTO> listPhieuNo = new ArrayList<>();
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetAllPhieuNoFromDatabase", null);
        PhieuNoDTO pn;
        while (result.next()) {
            pn = new PhieuNoDTO(result.getString("MAPHIEUNO"), result.getString("MAKHACHHANG"), result.getString("MAPHIEUBH"), 
                        result.getDate("NGAYNO"), result.getLong("SOTIENNO"), result.getDate("NGAYTRA"), result.getLong("SOTIENTRA"), result.getLong("CONLAI"));
            listPhieuNo.add(pn);
        }
        return listPhieuNo;
    }
    
    public ArrayList<PhieuNoDTO> GetPhieuNoByRule(PhieuNoDTO phieuno) {
        Object[] objs = { phieuno.getMaPhieuNo(), phieuno.getMaKhachHang(), phieuno.getMaPhieuBH(), 
            phieuno.getNgayNo(), phieuno.getSoTienNo(), phieuno.getNgayTra(), phieuno.getSoTienTra(), phieuno.getConLai()};
        ArrayList<PhieuNoDTO> listPhieuNo = new ArrayList<>();
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetPhieuNoFromDatabase(?, ?, ?, ?, ?, ?, ?, ?)", objs);
        PhieuNoDTO pn;
        try {
            while (result.next()) { 
                pn = new PhieuNoDTO(result.getString("MAPHIEUNO"), result.getString("MAKHACHHANG"), result.getString("MAPHIEUBH"),
                        result.getDate("NGAYNO"), result.getLong("SOTIENNO"), result.getDate("NGAYTRA"), result.getLong("SOTIENTRA"), result.getLong("CONLAI"));
                listPhieuNo.add(pn);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PhieuNoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listPhieuNo;
    }
    
    public int updatePhieuNO(PhieuNoDTO phieuno) {
        Object[] objs = { phieuno.getMaPhieuNo(), phieuno.getNgayTra(), phieuno.getSoTienTra(), phieuno.getConLai()};
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateDebtBillFromDatabase(?, ?, ?, ?)", objs);
    }
    
    public int DeletePhieuNO(PhieuNoDTO phieuno) {
        Object[] objs = { phieuno.getMaPhieuNo() };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteDebtBillFromDatabase( ? )", objs);
    }
    
    public int InsertPhieuNO(PhieuNoDTO phieuno) {
        Object[] objs = { phieuno.getMaPhieuNo(), phieuno.getMaKhachHang(), phieuno.getMaPhieuBH(), phieuno.getNgayNo(),
            phieuno.getSoTienNo(), phieuno.getNgayTra(), phieuno.getSoTienTra(), phieuno.getConLai()};
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertDebtBillFromDatabase(?, ?, ?, ?, ?, ?, ?, ?)", objs);
    }
}
